package kaka.controls.form
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Form;
	import mx.containers.Grid;
	import mx.containers.GridItem;
	import mx.containers.GridRow;
	import mx.containers.HBox;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.core.Application;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;
	import mx.validators.Validator;

	[Event(type="flash.events.Event",name="submit")]
	public class InfoForm extends Form
	{
		//public var saveIcon:Class = Icon.save;
		
		public var showSubmitBtn:Boolean;
		public var submitLabel:String = "提交";
		//XFormItem对象集合
		private var _infoFormItems:Array = [];
		//是否两列显示标记
		public var towColumn:Boolean = false;
		public var alternateLabelStyleName:String;
		public function InfoForm()
		{
			super(); 
			percentWidth=100;
			addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void
			{
				_created = true;
				for each(var child:Object in getChildren())
				{
					if(child is InfoFormItem)
					{
						_infoFormItems.push(child);
					}
				}
				for each(var xItem:InfoFormItem in _infoFormItems)
				{
					if(xItem.required)
					{
						var validator:Validator = new Validator();
						validator.required = true;
						validator.source = xItem.editor;
						validator.property = xItem.editorField;
						validators.push(validator);
					}
				}
				if(towColumn)
				{
					showInTwoColumn();
				}
				if(showSubmitBtn)
				{
					var submitBtn:Button = new Button();
					submitBtn.label = submitLabel;
					//submitBtn.setStyle("icon",saveIcon);
					var bbar:HBox = new HBox();
					bbar.percentWidth = 100;
					bbar.setStyle("horizontalAlign","center");
					bbar.addChild(submitBtn);
					defaultButton = submitBtn;
					addChild(bbar);
				}
				if(defaultButton)
				{
					defaultButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void
					{
						if(validate())
						{
							dispatchEvent(new Event("submit"));
						}
					}
					);
				}
			}
			);
		}
		
		
		
		private function showInTwoColumn():void{
			var grid:Grid = new Grid;
			grid.percentWidth = 100;
			var tmpGridItems:Array = [];
			for(var i:int = 0; i < _infoFormItems.length; i++)
			{
				if(Math.ceil((i+1) / 2) % 2 == 0)
				{
					(_infoFormItems[i] as InfoFormItem).setStyle("labelStyleName",alternateLabelStyleName);
				}
				var gridItem:GridItem = new GridItem;
				gridItem.percentWidth = 100;
				gridItem.addChild(_infoFormItems[i]);
				tmpGridItems.push(gridItem);
				if(tmpGridItems.length == 2)
				{
					var gridRow:GridRow = new GridRow;
					gridRow.percentWidth = 100;
					for each(gridItem in tmpGridItems)
					{
						gridRow.addChild(gridItem);
					}
					grid.addChild(gridRow);
					tmpGridItems.splice(0,2);
				}
			}
			addChildAt(grid, 0);
			var labelWidth:Number = maxLabelWidth;
			for each(var xItem:InfoFormItem in _infoFormItems)
			{
				if(labelWidth < xItem.itemLabel.measuredWidth)
				{
					labelWidth = xItem.itemLabel.measuredWidth;
				}
			}
			for each(xItem in _infoFormItems)
			{
				xItem.setStyle("labelWidth", labelWidth);
			}
			setStyle("labelWidth", labelWidth);
		}
		
		private var _created:Boolean = false;
		private var _data:Object = {};
		override public function set data(value:Object):void
		{
			super.data = value;
			_data = value;
			
			if(_created)
			{
				updateFormItems();
			}
			else
			{
				addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void
				{
					if(_created)
					{
						updateFormItems();
					}
				});
			}
		}
		
		public function updateFormItems():void{
			if(_data)
			{
				for each(var xItem:InfoFormItem in _infoFormItems)
				{
					try
					{
						var v:Object = _data[xItem.dataField];
						if(xItem.dataFun != null)
						{
							v = xItem.dataFun(v);
						}
						xItem.editor[xItem.editorField] = v;
					}catch(e:Error){
						trace(e.message);
					}
				}
			}
		}
		
		override public function get data():Object{
			for each(var xItem:InfoFormItem in _infoFormItems){
				try{
					if (xItem.dataField!=null)
					_data[xItem.dataField] = xItem.editor[xItem.editorField];
				}catch(e:Error){
					
				}
			}
			return _data;
		}
		
		private var validators:Array = [];
		//所有验证是否成功
		public function validate():Boolean
		{
			//验证集合内的验证器都成功，返回空数组
			return (Validator.validateAll(validators).length == 0);
		}
		
		public var win:TitleWindow;
		public function popUp():TitleWindow
		{
			if(win == null)
			{
				win = new TitleWindow();
				win.title = label;
				win.showCloseButton = true;
				win.addEventListener(CloseEvent.CLOSE, function(event:CloseEvent):void
				{
					PopUpManager.removePopUp(event.target as IFlexDisplayObject);
				});
				win.addChild(this);
				
				var bar:HBox = new HBox();
				bar.percentWidth = 100;
				var okBtn:Button = new Button;
				okBtn.label = submitLabel;
				//okBtn.setStyle("icon",saveIcon);
				bar.addChild(okBtn);
				var cancelBtn:Button = new Button;
				//cancelBtn.setStyle("icon",Icon.del);
				cancelBtn.label = "取消";
				cancelBtn.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void
				{
					closePopUp();
				});
				bar.addChild(cancelBtn);
				bar.setStyle("horizontalAlign","center");
				win.addChild(bar);
				
				this.defaultButton = okBtn;
				win.addEventListener(ResizeEvent.RESIZE, function(event:ResizeEvent):void
				{
					PopUpManager.centerPopUp(win);
				});
			}
			
			PopUpManager.addPopUp(win, Application.application as DisplayObject, true);
			PopUpManager.centerPopUp(win);
			return win;
		}
		
		public function closePopUp():void
		{
			
			if(win)
			{
				win.dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
			}
		}
		
		public function submit(submitListener:Function):void
		{
			addEventListener("submit", submitListener);
		}

		public function get infoFormItems():Array
		{
			return _infoFormItems;
		}

		public function set infoFormItems(value:Array):void
		{
			_infoFormItems = value;
			for each(var xItem:InfoFormItem in _infoFormItems)
			{
				if(xItem.required)
				{
					var validator:Validator = new Validator();
					validator.required = true;
					validator.source = xItem.editor;
					validator.property = xItem.editorField;
					validators.push(validator);
				}
			}
		}

	}
}